<template>
  <el-dialog v-if="dialogShow" v-model="dialogShow" title="算法选择" width="1000px">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true" size="small" label-width="80">
      <el-form-item label="分析类型" prop="displayName">
        <el-input v-model="queryParams.displayName" placeholder="请输入分析类型" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item style="padding-left: 80px">
        <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableDatas" highlight-current-row @current-change="handleCurrentChange">
      <el-table-column label="序号" width="50" type="index" align="center">
        <template #default="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分析名" align="center" prop="analysis_type"></el-table-column>
      <el-table-column label="类别" align="center" prop="loadType">
        <template #default="scope">
          <span>{{ scope.row.application_type }}/{{ scope.row.classification_type }}/{{ scope.row.material_type }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="pagenation">
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        layout="prev, pager, next,jumper"
        :total="total"
        @pagination="getList"
      />
    </div> -->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="handleSure">选择</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="AlgorithmSelectDialog" lang="ts">
import { asapGetAnalysisList, asapCreateAnalysisLoad } from '@/api/loadProcessingAlgorithm/loadTaskManagement/index';
const dialogShow = ref(false);
const currentRow = ref();
const taskInfo = ref({});
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  displayName: ''
});
const tableDatas = ref([]);
const loading = ref(false);
const btnLoading = ref(false);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emit = defineEmits(['on-sure']);
const openDialog = async (data) => {
  taskInfo.value = data;
  await getList();
  dialogShow.value = true;
};
const closeDialog = () => {
  dialogShow.value = false;
};
/** 查询模型列表 */
const getList = async () => {
  loading.value = true;
  const obj = {
    query_dict: {
      analysis_type: queryParams.displayName,
      calculate_object: [{ object_type: 'independent' }],
      type_list: [{ application_type: 'static_strength', classification_type: 'generalization', material_type: 'metal' }]
    }
  };
  const response = await asapGetAnalysisList(obj);
  tableDatas.value = response.data;
  total.value = response.total;
  loading.value = false;
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}
const handleCurrentChange = (val) => {
  currentRow.value = val;
};
const handleSure = () => {
  if (currentRow.value) {
    btnLoading.value = true;
    const obj = {
      task_id: taskInfo.value.id,
      version_id: taskInfo.value.taskForm.loadcaseVersionId,
      analysis_type: currentRow.value.analysis_type
    };
    asapCreateAnalysisLoad(obj).then((res) => {
      emit('on-sure', currentRow.value);
      closeDialog();
      btnLoading.value = false;
      proxy?.$modal.msgSuccess('选择成功！');
    });
  } else {
    proxy?.$modal.msgWarning('请选择数据！');
  }
};
defineExpose({
  openDialog
});
</script>

<style></style>
